package com.version1_3.controller;

import com.alibaba.fastjson.JSONObject;
import com.util.format.MapToJson;
import com.version1_3.service.ActivityService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.text.DecimalFormat;
import java.util.Map;

/**
 * Created by kuibu on 16/2/23.
 */
@Controller
public class ActivityController {
    private static final Logger LOGGER = Logger.getLogger(ActivityController.class);

    @Autowired
    private ActivityService activityService;


    /**
     * 活动主界面 ok
     * 调用接口: http://139.129.18.232:7080/kuibuversion1/activity/main/{userId}
     *
     * @return
     */
    @RequestMapping(value = "/activity/main/{userId}", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String activityMainPage(@PathVariable("userId") int userId) {
        return activityService.activityMainPage(userId);
    }

    /**
     * 活动详情页面 ok
     * 调用接口: http://139.129.18.232:7080/kuibuversion1/activity/activityInfo/{activityId}
     *
     * @param activityId
     * @return
     */
    @RequestMapping(value = "/activity/activityInfo/{activityId}", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String activityInfo(@PathVariable("activityId") int activityId) {
        return activityService.activityInfo(activityId);
    }

    /**
     * 我的活动 ok
     * 调用接口: http://139.129.18.232:7080/kuibuversion1/activity/myActivity/{userId}
     *
     * @param userId
     * @return
     */
    @RequestMapping(value = "/activity/myActivity/{userId}", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String myActivity(@PathVariable("userId") int userId) {
        return activityService.myActivity(userId);
    }


    /**
     * 参与活动 ok
     * 调用接口: http://139.129.18.232:7080/kuibuversion1/activity/joinActivity/{userId}/{activityId}
     *
     * @param userId
     * @param activityId
     * @return
     */
    @RequestMapping(value = "/activity/joinActivity/{userId}/{activityId}", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String joinActivity(@PathVariable("userId") int userId, @PathVariable("activityId") int activityId) {
        return activityService.joinActivity(userId, activityId);
    }

    /**
     * 活动打卡界面 ok
     * 调用接口: http://139.129.18.232:7080/kuibuversion1/activity/activityPage/{activityId}/{userId}
     *
     * @param activityId
     * @param userId
     * @return
     */
    @RequestMapping(value = "/activity/activityPage/{activityId}/{userId}", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String activityPage(@PathVariable("activityId") int activityId, @PathVariable("userId") int userId) {
        return activityService.activityPage(activityId, userId);
    }

    /**
     * 打卡 ok
     * 调用接口: http://139.129.18.232:7080/kuibuversion1/activity/launch
     *
     * @param jsonObject
     * @return
     */
    @RequestMapping(value = "/activity/launch", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String launchActivity(@RequestBody JSONObject jsonObject) {

        int activityId = 0;
        int userId = -1;
        try {
            Object data = jsonObject.get("data");
            activityId = jsonObject.getIntValue("activityId");
            userId = jsonObject.getIntValue("userId");

            return activityService.launchActivity(activityId, userId, data);

        } catch (Exception exception) {
            LOGGER.error("接收客户端活动打卡信息错误,发送打卡信息: " + jsonObject.toJSONString(), exception);
            return "{\"launchResult\":false}";
        }
    }

    /**
     * 分享打卡信息
     * 调用接口: http://139.129.18.232:7080/kuibuversion1/activity/shareLauchCardInfo/{activityId}/{userId}
     */
    @RequestMapping(value = "/activity/shareLauchCardInfo/{activityId}/{userId}", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
    @ResponseBody
    public ModelAndView shareLauchCardInfo(@PathVariable("activityId") int activityId, @PathVariable("userId") int userId) {
        ModelAndView modelAndView = new ModelAndView("/activity/lauchCardInfo");

        activityService.recordActivityShare(userId, activityId);
        Map<String, Object> shareLaunchCardInfoMap = activityService.getShareLaunchCardInfo(userId, activityId);

        if (activityId == 11) {
            modelAndView = new ModelAndView("/activity/gongyipao");
            if (shareLaunchCardInfoMap == null) {
                modelAndView.addObject("minute", 0);
                modelAndView.addObject("second", 0);
                modelAndView.addObject("money", 0);
            } else {
                int time = (int) shareLaunchCardInfoMap.get("timeCount");
                modelAndView.addObject("minute", (int) (time / 60));
                modelAndView.addObject("second", (int) (time % 60));
                modelAndView.addObject("money", shareLaunchCardInfoMap.get("money"));
            }

            modelAndView.addObject("activityId", activityId);
            modelAndView.addObject("school", shareLaunchCardInfoMap.get("school")); // 学校
            return modelAndView;
        } else {
            if (shareLaunchCardInfoMap == null) {
                // 跬步公益跑
                modelAndView.addObject("mile", 0); // 今日里程
                modelAndView.addObject("mileCount", 0); // 总里程
                modelAndView.addObject("money", 0); // 总计捐款金额
                modelAndView.addObject("timeCount", 0);

                // 平板支撑
                modelAndView.addObject("seconds", 0); // 坚持秒数
                modelAndView.addObject("secondsCount", 0); // 总计坚持时间
                modelAndView.addObject("money", 0); // 总计捐款金额

                // 俯卧撑 | 仰卧起坐
                modelAndView.addObject("number", 0); // 个数
                modelAndView.addObject("numberCount", 0); // 总计个数
                modelAndView.addObject("money", 0); // 总计捐款金额

                modelAndView.addObject("activityId", activityId);
                modelAndView.addObject("school", "未知"); // 学校
                return modelAndView;
            } else {
                if (activityId == 13) {
                    // 平板支撑
                    modelAndView.addObject("seconds", shareLaunchCardInfoMap.get("seconds")); // 坚持秒数
                    modelAndView.addObject("secondsCount", shareLaunchCardInfoMap.get("secondsCount")); // 总计坚持时间
                    modelAndView.addObject("money", shareLaunchCardInfoMap.get("money")); // 总计捐款金额
                } else {
                    // 俯卧撑 | 仰卧起坐
                    modelAndView.addObject("number", shareLaunchCardInfoMap.get("number")); // 个数
                    modelAndView.addObject("numberCount", shareLaunchCardInfoMap.get("numberCount")); // 总计个数
                    modelAndView.addObject("money", shareLaunchCardInfoMap.get("money")); // 总计捐款金额
                }

                modelAndView.addObject("activityId", activityId);
                modelAndView.addObject("school", shareLaunchCardInfoMap.get("school")); // 学校
                return modelAndView;
            }
        }

    }

    /**
     * 记录用户分享打卡信息积分
     *
     * @param activityId
     * @param userId
     * @return
     */
    @RequestMapping(value = "/activity/recordShareLaunchCardInfo/{activityId}/{userId}", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String recordShareLaunchCardInfo(@PathVariable("activityId") int activityId,
                                            @PathVariable("userId") int userId) {
        boolean recordShareLaunchCardInfoResult = activityService.recordActivityShare(userId, activityId);

        return new MapToJson("recordShareLaunchCardInfoResult", recordShareLaunchCardInfoResult).toJsonString();
    }

    /**
     * 设置用户身高体重信息
     * 调用接口: http://139.129.18.232:7080/kuibuversion1/activity/setBodyInfo
     */
    @RequestMapping(value = "/activity/setBodyInfo", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String setUserBodyInfo(@RequestBody JSONObject jsonObject) {
        int userId = -1;
        double height = 0;
        double weight = 0;
        String name = "";
        String school = "";
        try {
            try {
                name = jsonObject.getString("name");
                school = jsonObject.getString("school");

                if (name == null) {
                    name = "";
                }

                if (school == null) {
                    school = "";
                }
            } catch (Exception e) {
                LOGGER.info("no key called name or school");
            }

            userId = jsonObject.getIntValue("userId");
            height = jsonObject.getDouble("height");
            weight = jsonObject.getDouble("weight");
        } catch (Exception excepiton) {
            LOGGER.error("设置用户身高体重信息,客户端发送参数格式错误,jsonObject: " + jsonObject.toJSONString());
            return "{\"code\":400}";
        }

        return activityService.setBodyInfo(userId, height, weight, name, school);
    }


}
